﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
//using YJKRevitDimension.Properties;

namespace YJKRevitDimension.SymbolTextSetting
{
	public partial class SymbolTextForm : System.Windows.Forms.Form
	{
		private GridCollector GridCollector { get; set; }

		private IndexMarkCollector IndexMarkCollector { get; set; }

		public SymbolTextForm()
		{
			this.InitializeComponent();
		}

		public SymbolTextForm(GridCollector gridCollector, IndexMarkCollector indexMarkCollector)
		{
			this.GridCollector = gridCollector;
			this.IndexMarkCollector = indexMarkCollector;
			this.InitializeComponent();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnSure_Click(object sender, EventArgs e)
		{
			if (this.radInCircle.Checked)
			{
				new GridFontSetting(this.GridCollector, GridAndIndexMarkOper.InTheCircle).DoSetGridAndIndexMarkStyle();
				new IndexMarkFontSetting(this.IndexMarkCollector, GridAndIndexMarkOper.InTheCircle).DoSetGridAndIndexMarkStyle();
			}
			else if (this.radOldStyle.Checked)
			{
				new GridFontSetting(this.GridCollector, GridAndIndexMarkOper.OldStyle).DoSetGridAndIndexMarkStyle();
				new IndexMarkFontSetting(this.IndexMarkCollector, GridAndIndexMarkOper.OldStyle).DoSetGridAndIndexMarkStyle();
			}
			else if (this.radOutCircle.Checked)
			{
				new GridFontSetting(this.GridCollector, GridAndIndexMarkOper.OutTheCircle).DoSetGridAndIndexMarkStyle();
				new IndexMarkFontSetting(this.IndexMarkCollector, GridAndIndexMarkOper.OutTheCircle).DoSetGridAndIndexMarkStyle();
			}
			base.Close();
		}

		private void SymbolTextForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.GetData();
		}

		private void radInCircle_Click(object sender, EventArgs e)
		{
            this.picThumbnails.Image = YJKRevitDimension.Properties.Resources.InCircle;
		}

		private void radOldStyle_Click(object sender, EventArgs e)
		{
            this.picThumbnails.Image = YJKRevitDimension.Properties.Resources.OldStyle;
		}

		private void radOutCircle_Click(object sender, EventArgs e)
		{
            this.picThumbnails.Image = YJKRevitDimension.Properties.Resources.OutCircle;
		}

		private void SaveData()
		{
			FileStream fileStream = new FileStream(new DirectoryInfo(Environment.GetEnvironmentVariable("TEMP")).FullName + "\\SymbolTextFormData.txt", FileMode.OpenOrCreate);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.Flush();
			streamWriter.WriteLine(this.radInCircle.Checked);
			streamWriter.WriteLine(this.radOutCircle.Checked);
			streamWriter.WriteLine(this.radOldStyle.Checked);
			streamWriter.Close();
			fileStream.Close();
		}

		private bool GetData()
		{
			bool result;
			try
			{
				FileStream fileStream = new FileStream(Ultility.GetUserTempDirectory() + "\\SymbolTextFormData.txt", FileMode.OpenOrCreate);
				StreamReader streamReader = new StreamReader(fileStream);
				if (streamReader.ReadLine() == "True")
				{
					this.radInCircle.Checked = true;
					this.picThumbnails.Image = YJKRevitDimension.Properties.Resources.InCircle;
				}
				else if (streamReader.ReadLine() == "True")
				{
					this.radOutCircle.Checked = true;
					this.picThumbnails.Image = YJKRevitDimension.Properties.Resources.OutCircle;
				}
				else
				{
					this.radOldStyle.Checked = true;
					this.picThumbnails.Image = YJKRevitDimension.Properties.Resources.OldStyle;
				}
				streamReader.Close();
				fileStream.Close();
				result = true;
			}
			catch (Exception)
			{
				this.radOldStyle.Checked = true;
				this.picThumbnails.Image = YJKRevitDimension.Properties.Resources.OldStyle;
				result = false;
			}
			return result;
		}

		private void SymbolTextForm_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
				return;
			}
		}

		private void SymbolTextForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SaveData();
		}
	}
}
